package com.example.dou2204.frag;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.dou2204.R;
import com.example.dou2204.adapter.FragAdapter;
import com.example.dou2204.baseMVVM.BaseViewFragment;
import com.example.dou2204.databinding.FragmentHomeBinding;
import com.example.dou2204.entity.MyCustomTabEntity;
import com.example.dou2204.frag.home.ShopFragment;
import com.example.dou2204.frag.home.TuijianFragment;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends BaseViewFragment<FragmentHomeBinding> {

    List<Fragment> list;
    ArrayList<CustomTabEntity> tabs;
    FragAdapter fragAdapter;

    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        list = new ArrayList<>();//防止出现数据重复加载的情况
        list.add(new TuijianFragment());
        list.add(new ShopFragment());
        fragAdapter = new FragAdapter(getActivity(),list);
        binding.vp.setAdapter(fragAdapter);
        tabs = new ArrayList<>();
        tabs.add(new MyCustomTabEntity("推荐",0,0));
        tabs.add(new MyCustomTabEntity("商城",0,0));
        binding.common.setTabData(tabs);

        //上下联动
        binding.vp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                binding.common.setCurrentTab(position);
            }
        });
        binding.common.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                binding.vp.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });


    }

    @Override
    protected int bindLayout() {
        return R.layout.fragment_home;
    }
}